/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_01_basic_grammar.Example_05_FlowControl.java]
 * Creation Date 	[14-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_01_basic_grammar;

/**
 * Java 流程控制
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_05_FlowControl {

	/**
	 * 目标值
	 */
	public static final int TARGET_NUMBER = 10;

	/**
	 * 通过if语句和递归方法使n的值逐渐变为TARGET_NUMBER
	 * 
	 * @param n
	 *            输入的参数
	 */
	public void ifStatement(int n) {
		System.out.print(n + " ");
		if (n == TARGET_NUMBER) {
			System.out.println();
		} else if (n > TARGET_NUMBER) {
			ifStatement(--n);
		} else if (n < TARGET_NUMBER) {
			ifStatement(++n);
		}
	}

	/**
	 * 通过for循环将n的值逐渐变为TARGET_NUMBER
	 * 
	 * @param n
	 *            输入的参数
	 */
	public void forStatement(int n) {
		for (; n > TARGET_NUMBER; n--) {
			System.out.print(n + " ");
		}
		for (; n < TARGET_NUMBER; n++) {
			System.out.print(n + " ");
		}
		System.out.println(n);
	}

	/**
	 * 通过while循环将n的值逐渐变为TARGET_NUMBER
	 * 
	 * @param n
	 *            输入的参数
	 */
	public void whileStatement(int n) {
		while (n < TARGET_NUMBER) {
			System.out.print(n++ + " ");
		}
		while (n > TARGET_NUMBER) {
			System.out.println(n-- + " ");
		}
		System.out.println(n);
	}

	/**
	 * 通过do...while循环将n的值逐渐变为TARGET_NUMBER
	 * 
	 * @param n
	 *            输入的参数
	 */
	public void doWhileStatement(int n) {
		if (n > TARGET_NUMBER) {
			do {
				System.out.print(n-- + " ");
			} while (n > TARGET_NUMBER);
		} else if (n < TARGET_NUMBER) {
			do {
				System.out.print(n++ + " ");
			} while (n < TARGET_NUMBER);
		}
		System.out.println(n);
	}

	/**
	 * switch结构
	 * 
	 * @param n
	 *            输入的参数
	 */
	public void switchStatement(int n) {
		switch (n) {
		case 10:
			System.out.println("n=10");
			break;
		case 9:
			System.out.println("n=9");
			break;
		case 8:
			System.out.println("n=8");
			break;
		default:
			System.out.println("n!=8 and n!=9 and n!=10");
			break;
		}
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Example_05_FlowControl control = new Example_05_FlowControl();
		control.ifStatement(6);
		control.forStatement(16);
		control.whileStatement(6);
		control.doWhileStatement(16);
		control.switchStatement(8);
	}

}
